using System.Collections.Generic;
using BuildWheel;
using UnityEngine;
using UnityEngine.UIElements;
using BaseInspector = UI.Inspector.BaseInspector;

public class ResInspector:BaseInspector
{
    public Dictionary<string, VisualElement> typeDic = new Dictionary<string, VisualElement>();
    public ResInspector(VisualElement inspector, Transform _ransform):base(_ransform,inspector,"Res")
    {
        typeDic["texture"] = uiElement.Q<VisualElement>("texture");
        typeDic["sprite"] = uiElement.Q<VisualElement>("texture");
        typeDic["audioClip"] = uiElement.Q<VisualElement>("audioClip");
        typeDic["text"] = uiElement.Q<VisualElement>("text");

        typeDic["model"] = uiElement.Q<VisualElement>("model");
        typeDic["prefab"] = uiElement.Q<VisualElement>("prefab");

        typeDic["anim"] = uiElement.Q<VisualElement>("normal");
        typeDic["controller"] = uiElement.Q<VisualElement>("normal");
        
        uiElement.Q<TextField>("ab").EnableWebGLInput();
        uiElement.Q<TextField>("path").EnableWebGLInput();
        uiElement.Q<TextField>("name").EnableWebGLInput();
    }

    void HideAll()
    {
        foreach (var item in typeDic)
        {
            item.Value.style.display = DisplayStyle.None;
        }
    }

    void ShowType(string t)
    {
        foreach (var item in typeDic)
        {
            if (t.Equals(item.Key))
            {
                item.Value.style.display = DisplayStyle.Flex;
            }
            else
            {
                //    item.Value.style.display = DisplayStyle.None;
            }
        }
    }

    private File selectFile;

    public override void Ref(File file)
    {
        if (selectFile == file) return;
        selectFile = file;
        HideAll();
        uiElement.Q<Label>("type").text = $"Type: {file.t}";
        uiElement.Q<TextField>("ab").value = $"{file.k}.webgl";
        uiElement.Q<TextField>("path").value = $"{file.k}.{file.s}";
        uiElement.Q<TextField>("name").value = file.n;
        switch (file.t)
        {
            case "texture":
            case "sprite":
                SpritePlane(file);
                break;
            case "audioClip":
                AudioClipPlane(file);
                break;
            case "text":
                TextPlane(file);
                break;

            case "model":
                ModelPlane(file);
                break;
            case "prefab":
                PrefabPlane(file);
                break;
            case "anim":
            case "controller":
                animPlane(file);
                break;
        }

        ShowType(file.t);
    }

    #region sprite

    void SpritePlane(File file)
    {
        var texture = typeDic[file.t];
        var content = texture.Q<VisualElement>("content");
        content.style.display = DisplayStyle.None;
        switch (file.t)
        {
            case "sprite":
                ResExtendsMgr.LoadAsync<Sprite>(file.k,
                    obj =>
                    {
                        content.style.backgroundImage = new StyleBackground(obj);
                        content.style.display = DisplayStyle.Flex;
                    });
                break;
            case "texture":
                ResExtendsMgr.LoadAsync<Texture2D>(file.k,
                    obj =>
                    {
                        content.style.backgroundImage = new StyleBackground(obj);
                        content.style.display = DisplayStyle.Flex;
                    });
                break;
        }
    }

    #endregion

    #region text

    void TextPlane(File file)
    {
        var ui = typeDic[file.t];
        ResExtendsMgr.LoadAsync<TextAsset>(file.k, obj =>
        {
            if (obj != null)
            {
                ui.Q<Label>("content").text = obj.text;
            }
            else
            {
                ui.Q<Label>("content").text = "nothing";
            }
        });
    }

    #endregion

    #region audioClip

    private AudioSource _AudioSource;

    void AudioClipPlane(File file)
    {
        var ui = typeDic[file.t];
        ui.Q<Button>("content").RegisterCallback<MouseDownEvent>((e) =>
        {
            if (_AudioSource.clip != null)
            {
                _AudioSource.Play();
            }
        });
        if (_AudioSource == null)
            _AudioSource = transform.Find("audioClip").GetComponent<AudioSource>();
        ResExtendsMgr.LoadAsync<AudioClip>(file.k, (clip =>
        {
            _AudioSource.clip = clip;
            _AudioSource.Play();
        }));
    }

    #endregion

    #region model

    void PrefabPlane(File file)
    {
        var ui = typeDic[file.t];
        ui.Q<VisualElement>("content").style.display = DisplayStyle.None;
        ResExtendsMgr.LoadAsync<GameObject>(file.k, (obj =>
        {
            if (obj != null)
            {
                CameraAutoFitter.Instance.LoadAndFitModel(obj);
                ui.Q<VisualElement>("content").style.backgroundImage =
                    new StyleBackground(Background.FromRenderTexture(CameraAutoFitter.Instance.targetTexture));
                ui.Q<VisualElement>("content").style.display = DisplayStyle.Flex;
            }
        }));
    }

    void ModelPlane(File file)
    {
        var ui = typeDic[file.t];
        ui.Q<VisualElement>("content").style.display = DisplayStyle.None;
        ResExtendsMgr.LoadAsync<GameObject>(file.k, (obj =>
        {
            if (obj != null)
            {
                CameraAutoFitter.Instance.LoadAndFitModel(obj);
                ui.Q<VisualElement>("content").style.backgroundImage =
                    new StyleBackground(Background.FromRenderTexture(CameraAutoFitter.Instance.targetTexture));
                ui.Q<VisualElement>("content").style.display = DisplayStyle.Flex;

                ui.Q<Vector3Field>("center").value = CameraAutoFitter.Instance.bounds.center;
                ui.Q<Vector3Field>("size").value = CameraAutoFitter.Instance.bounds.size;
            }
        }));
    }

    #endregion

    void animPlane(File file)
    {
        var ui = typeDic[file.t];
    }
}